package com.soup.common.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.google.common.collect.Lists;
import com.soup.common.constant.CommonConstant;
import com.soup.common.utils.DateUtil;
import com.soup.common.support.filter.RequestLoggerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * web mvc config
 *
 * @author zhaoyi
 */
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired(required = false)
    private RequestLoggerInterceptor requestLoggerInterceptor;

    @Autowired(required = false)
    private JwtProperties jwtProperties;

    /**
     * 开启跨域
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路由
        registry.addMapping("/**")
                // 设置允许跨域请求的域名
                .allowedOrigins("*")
                // 是否允许证书（cookies）
                .allowCredentials(true)
                // 设置允许的方法
                .allowedMethods("*")
                // 设置允许的响应头
                .allowedHeaders("*")
                // 暴露response的响应头，可以让跨域请求js获取到的response header
                .exposedHeaders(jwtProperties.getHeader())
                // 跨域允许时间, 默认3600
                .maxAge(CommonConstant.DEFAULT_CORS_MAX_AGE);
    }

    /**
     * 添加
     * @param registry InterceptorRegistry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 对所有请求进行log拦截
        registry.addInterceptor(requestLoggerInterceptor).addPathPatterns("/**");
    }

    /**
     * 替换默认json解析器为fastjson
     *
     * @param converters 转换器
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new ByteArrayHttpMessageConverter());

        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        config.setCharset(StandardCharsets.UTF_8);
        config.setDateFormat(DateUtil.dateFormate);
        // 设置允许返回为null的属性
        config.setSerializerFeatures(SerializerFeature.WriteNullListAsEmpty);
        fastJsonConverter.setFastJsonConfig(config);
        List<MediaType> list = Lists.newArrayList(MediaType.APPLICATION_JSON_UTF8);
        fastJsonConverter.setSupportedMediaTypes(list);
        converters.add(fastJsonConverter);
    }
}
